package com.zhou.common.mybatis.template.shiro.zhou.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhou.common.mybatis.template.shiro.zhou.entity.User;
import com.zhou.common.mybatis.template.shiro.zhou.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private UserMapper userMapper;

    @PreAuthorize("hasRole('USER')")
    @GetMapping("/test")
    public String test(){
        PageHelper.startPage(1, 11);
        List<User> domes = userMapper.selectList(Wrappers.emptyWrapper());
        PageInfo<User> userPageInfo = new PageInfo<>(domes);
        return "hello USER!";
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("test1")
    public String test1(){
        if(1==1){
            throw new RuntimeException("测试出错");
        }
        return "hello ADMIN!";
    }


    @Autowired(required = false)
    public DefaultMQProducer defaultProducer;

    @PostMapping("/push/message")
    public String test2(String message) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        Message sendMsg = new Message("topic-test", "TestTag", message.getBytes());
        SendResult result = defaultProducer.send(sendMsg);
        log.info("消息发送的返回值={}",result);
        return "发送成功";
    }

    /**
     *
     * @param message
     * @return
     * @throws InterruptedException
     * @throws RemotingException
     * @throws MQClientException
     * @throws MQBrokerException
     */
    @PostMapping("/push/delay")
    public String sendMessageDelay(String message) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        Message sendMsg = new Message("topic-test", "TestTag", message.getBytes());
        sendMsg.setDelayTimeLevel(2);
        SendResult result = defaultProducer.send(sendMsg);
        log.info("消息发送的返回值={}",result);
        return "发送成功";
    }

    @PostMapping("/push/order")
    public String sendMessageOrder(String message,String shardingKey) throws InterruptedException, RemotingException, MQClientException, MQBrokerException, UnsupportedEncodingException {

        String body="{\"processInstanceId\":\"7384b5e5af0f11ec8b6600163e354323\",\"task\":{\"applyTime\":1648524144147,\"applyUserId\":\"1634022739191344\",\"applyUserName\":\"李嘉欣\",\"commandCode\":\"createWorkOrder\",\"copiers\":[],\"modelName\":\"zhaonian测试流程1\",\"nodeAuth\":{\"addSignature\":\"false\",\"delay\":\"false\",\"transfer\":\"false\",\"stop\":\"false\",\"circulation\":\"false\",\"signIn\":\"false\",\"callback\":\"false\",\"urge\":\"{\\\"urgeStartTime\\\":\\\"quarterHour\\\",\\\"urgeInterval\\\":\\\"oneMinute\\\",\\\"switch\\\":false}\"},\"participants\":[{\"participantId\":\"36105\",\"participantName\":\"审批中心管理员\",\"participantType\":\"USR\"}],\"processInstanceId\":\"7384b5e5af0f11ec8b6600163e354323\",\"serial\":\"1\",\"tacheCode\":\"approval_first\",\"tacheName\":\"用户任务\",\"timestamp\":1648524145883,\"workItemId\":\"744ebcbaaf0f11ec8b6600163e354323\"},\"type\":\"todo\"}";

        Message sendMsg = new Message("topic-test", "TestTag", body.getBytes("UTF-8"));
        sendMsg.setDelayTimeLevel(2);
        SendResult result = defaultProducer.send(sendMsg);

        log.info("消息发送的返回值={}",result);
        return "发送成功";
    }
}
